/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-06-13
*/
package com.rzico.order.controller.member;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.ProductService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.Order;
import com.rzico.order.entity.OrderReview;
import com.rzico.order.enumx.OrderStatusEnum;
import com.rzico.order.model.ReviewMeta;
import com.rzico.order.service.OrderReviewService;
import com.rzico.order.service.OrderService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单评论控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-06-13
 */
@Api(description = "订单评论接口")
@RestController
@RequestMapping("/member/orderReview")
public class OrderReviewController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderReviewService orderReviewService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    /**
     * 分页查询订单评论
     *
     * @return
    */
    @ApiOperation("分页查询订单评论")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "productId", value = "商品Id", dataType = "String", paramType = "query")
    })
    public CommResult<OrderReview> list(Long productId,Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (productId!=null){

            Product product = productService.findById(productId);
            if (product!=null) {
                params.put("goodsId", product.getGoodsId());
            }
        }

        Member member = memberService.getCurrent();
        if (member!=null) {
            params.put("memberId", member.getId());
        } else {
            return CommResult.error("没有登录");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderReview> list = orderReviewService.selectList(params);
        PageResult<OrderReview> pageResult = new PageResult<OrderReview>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 保存评论
     *
     * @param orderReviews
     * @return
     */
    @ApiOperation("保存订单评论")
    @PostMapping("/save")
    public CommResult<OrderReview> save(@RequestBody List<OrderReview> orderReviews) {

        Member member = memberService.getCurrent();
        if (member==null) {
            return CommResult.error("没有登录");
        }

        Long orderId = null;

        for (OrderReview orderReview:orderReviews) {

            orderReview.setContent(StringUtils.filterEmoji(orderReview.getContent()));

            orderReview.setMemberId(member.getId());
            orderReview.setIsMetaVideo(false);
            orderReview.setMeta(null);
            if (orderReview.getItemList() != null) {
                orderReview.setMeta(JSON.toJSONString(orderReview.getItemList()));
            }
            for (ReviewMeta meta : orderReview.getItemList()) {
                if (meta.getType().equals(2)) {
                    orderReview.setIsMetaVideo(true);
                }
            }
            orderReview.setCreateDate(new Date());
            int affectCount = orderReviewService.insertUseGeneratedKeys(orderReview);
            if (affectCount <= 0) {
                return CommResult.error();
            }

            if (orderReview.getOrderId() != null) {
              orderId = orderReview.getOrderId();
            }

        }

        if (orderId != null) {
            Order order = orderService.findById(orderId);
            if (order.getOrderStatus().equals(OrderStatusEnum.ORDER_STATUS_COMPLETE.getId())) {
                order.setOrderStatus(OrderStatusEnum.ORDER_STATUS_REVIEWED.getId());
                orderService.updateByPrimaryKeySelective(order);
            }
        }

        return CommResult.success();

    }


    /**
     * 批量删除订单评论
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除订单评论", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除订单评论,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<OrderReview> del(@PathVariable String ids) {

        orderReviewService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

}
